package com.geezdata.cps.base.statemachine;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;

public abstract class AbstractState<E extends Stateable> implements State<E> {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public State<E> transfer(E entity, Map<String, Object> context, StateAction<E> action) {
        logger.info("Close {}", this.getClass().getSimpleName());
        this.onClose(entity, context);

        State<E> newState = action.execute();

        logger.info("Entry {}", newState.getClass().getSimpleName());
        newState.onEntry(entity, context);

        return newState;
    }

    public void unsupport() {
    	logger.error("Unsupport state transfer.");
        throw new MyException(ErrorCode.INTERNAL_SERVER_ERROR);
    }
}
